using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;

namespace LdExpress
{
    public class Module
    {
        // Fields
        public string DependOn;
        public string ID;
        public int IsAlwaysUsed;
        public int IsUsed;
        public string Name;
        public string VersionID;

        // Methods
        public Module()
        {
            this.ID = "";
            this.Name = "";
            this.VersionID = "";
            this.IsUsed = 1;
            this.IsAlwaysUsed = 0;
            this.DependOn = "";
        }

        public Module(string strID, string strName, string strVersionID, int nIsUsed, int nIsAlwaysUsed, string strDependOn)
        {
            this.ID = "";
            this.Name = "";
            this.VersionID = "";
            this.IsUsed = 1;
            this.IsAlwaysUsed = 0;
            this.DependOn = "";
            this.ID = strID;
            this.Name = strName;
            this.VersionID = strVersionID;
            this.IsUsed = nIsUsed;
            this.IsAlwaysUsed = nIsAlwaysUsed;
            this.DependOn = strDependOn;
        }
//        CREATE TABLE `tbl_moduleupdate` (
//  `ID` VARCHAR(50) NOT NULL,
//  `Name` VARCHAR(255) DEFAULT NULL,
//  `IsUsed` INT(11) DEFAULT NULL,
//  `IsAlwaysUsed` INT(11) DEFAULT NULL,
//  `DependOn` VARCHAR(255) DEFAULT NULL,
//  `VersionID` VARCHAR(100) DEFAULT NULL,
//  PRIMARY KEY (`ID`)
//) ENGINE=INNODB DEFAULT CHARSET=utf8
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            DBModule.ExecuteNonQuery("Delete from tbl_moduleupdate WHERE ID='" + this.ID + "'", cn, trans);
        }

        public DataSet GetList(string OrderBy, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "select * from tbl_moduleupdate";
            if ((OrderBy != null) && (OrderBy != ""))
            {
                strSQL = strSQL + " Order by " + OrderBy;
            }
            return DBModule.ExecuteQuery(strSQL, cn, trans);
        }

        public static bool isModuleExisting(string strID, MySqlConnection cn, MySqlTransaction trans)
        {
            return (int.Parse(DBModule.ExecuteQueryForOneResult("select count(*) as NumOfRecords from tbl_moduleupdate where ID='" + strID + "'", cn, trans)) > 0);
        }

        public int Load(MySqlConnection cn, MySqlTransaction trans)
        {
            return this.Load(this.ID, cn, trans);
        }

        public int Load(string ModuleID, MySqlConnection cn, MySqlTransaction trans)
        {
            DataSet ds = DBModule.ExecuteQuery("select * from tbl_moduleupdate where ID='" + ModuleID + "' limit 1", cn, trans);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID"))
                {
                    this.ID = dr["ID"].ToString();
                }
                if (!dr.IsNull("Name"))
                {
                    this.Name = dr["Name"].ToString();
                }
                if (!dr.IsNull("VersionID"))
                {
                    this.VersionID = dr["VersionID"].ToString();
                }
                if (!dr.IsNull("IsUsed"))
                {
                    this.IsUsed = int.Parse(dr["IsUsed"].ToString());
                }
                if (!dr.IsNull("IsAlwaysUsed"))
                {
                    this.IsAlwaysUsed = int.Parse(dr["IsAlwaysUsed"].ToString());
                }
                if (!dr.IsNull("DependOn"))
                {
                    this.DependOn = dr["DependOn"].ToString();
                }
                return 1;
            }
            return -1;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            bool bExist = isModuleExisting(this.ID, cn, trans);
            string strSQL = "";
            if (bExist)
            {
                strSQL = string.Concat(new object[] { "Update tbl_moduleupdate Set Name=N'", this.Name, "',VersionID='", this.VersionID, "', IsUsed=", this.IsUsed, ", IsAlwaysUsed=", this.IsAlwaysUsed, ", DependOn='", this.DependOn, "' WHERE ID='", this.ID, "'" });
            }
            else
            {
                strSQL = string.Concat(new object[] { "insert into tbl_moduleupdate(ID,Name,VersionID,IsUsed,IsAlwaysUsed,DependOn)  Values('", this.ID, "',N'", this.Name, "','", this.VersionID, "',", this.IsUsed.ToString(), ",", this.IsAlwaysUsed, ",'", this.DependOn, "')" });
            }
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
        }
    }
}
